#include "am_tone_player.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "audio/pcm/audio_pcm.h"
#include "audio/manager/audio_manager.h"
#include "soundStreamControl.h"
#include "audio/manager/audio_manager.h"
#include "common/framework/fs_ctrl.h"
#include "kernel/os/os.h"


int isCompleted = 0;
static unsigned char is_end_play = 1;

static void player_demo_callback(player_events event, void *data, void *arg)
{
    switch (event) {
    case PLAYER_EVENTS_MEDIA_PREPARED:
        printf("media is prepared. play media now.\n");
        printf("you can use player to seek, get duration(by size), get current position(by tell) from now on.\n");
        break;
    case PLAYER_EVENTS_MEDIA_STOPPED:
	case PLAYER_EVENTS_TONE_STOPED:
        printf("media is stopped by user.\n");
        isCompleted = 1; is_end_play = 1;
        break;
    case PLAYER_EVENTS_MEDIA_ERROR:
	case PLAYER_EVENTS_TONE_ERROR:
        printf("error occur\n");
        isCompleted = 1; is_end_play = 1;
        break;
    case PLAYER_EVENTS_MEDIA_PLAYBACK_COMPLETE:
	case PLAYER_EVENTS_TONE_COMPLETE:
        printf("media play is completed\n");
        isCompleted = 1; is_end_play = 1;
        break;
    default:
        break;
    }
}



#ifdef TENCENT_ONLINE
static tone_base *player;
static player_base  *player_in;
int  am_tone_player_init(player_base *in_player)
{

    player = player_tone_create(in_player, player_demo_callback, NULL);
    player_in = in_player;

}
#else
static player_base *player;
int  am_tone_player_init()
{

     player = player_create();
	 if (player == NULL) {
		 printf("player create fail.");
		 return;
	 }
     else
         printf("player create successed.");



}
#endif



int  am_tone_player_defaultplay(default_music_list_t def_music)
{
       int ret = 0;
       char url[70] = {0};
       sprintf(url,"%s%d%s%d","flash://0?addr=",defaule_list[def_music-1].start_address,"&length=",defaule_list[def_music-1].music_len);
        do
	   	{
	      OS_MSleep(100);
	   }while(is_end_play == 0); //等待播放结束
	   isCompleted = 0;
	   is_end_play = 0;
	   #ifdef TENCENT_ONLINE
       player_in->play_tone(player_in, player, url);
	   printf("play  tone end>>>>>>>>>>>>>>>>\r\n");
	   #else
       player->set_callback(player, player_demo_callback, NULL);
	   ret = player->play(player, url);
	   if (ret != 0) {
            return -1;
	   	}
	   #endif
	   while (!isCompleted) {
	   OS_MSleep(50);
       }
       /* stop it */
       return 0;
      
}



int  am_tone_player_play(int start_address, int music_len)
{

	int ret = 0;
	char url[70] = {0};
	sprintf(url,"%s%d%s%d","flash://0?addr=",start_address,"&length=",music_len);
    do
   	{
      OS_MSleep(100);
    }while(is_end_play == 0); //等待播放结束
    isCompleted = 0;
    is_end_play = 0;
#ifdef TENCENT_ONLINE
	player_in->play_tone(player_in, player, url);
#else
	player->set_callback(player, player_demo_callback, NULL);
	ret = player->play(player, url);
	if (ret != 0) {
		 return -1;
	 }
#endif

	while (!isCompleted) {
	OS_MSleep(50);
	}
	return 0;



}



